package cn.dankal.flyaudio.personal;

import android.content.Intent;
import android.text.Editable;
import android.text.Selection;
import android.widget.EditText;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.UserInfoBean;
import cn.dankal.basiclib.rx.AbstractDialogSubscriber;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.pojo.ReasonBean;
import cn.dankal.flyaudio.R;


public class EditNameActivity extends BaseActivity {


    @BindView(R.id.et_name)
    EditText mEtName;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_name;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("编辑昵称");
        mEtName.setText(DKUserManager.getUserInfo().getName());
        Editable text = mEtName.getText();
        Selection.setSelection(text, text.length());

    }


    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        String name = mEtName.getText().toString();
        if (!StringUtil.isValid(name)) {
            ToastUtils.showShort("请填写姓名");
            return;
        }

        UserServiceFactory.changeName(name)
                .subscribe(new AbstractDialogSubscriber<ReasonBean>(this) {
                    @Override
                    public void onNext(ReasonBean reasonBean) {
                        UserInfoBean userInfoBean = DKUserManager.getUserInfo();
                        userInfoBean.setName(name);
                        DKUserManager.updateUserInfo(userInfoBean);
                        Intent data = new Intent();
                        data.putExtra("编辑昵称", name);
                        setResult(RESULT_OK, data);
                        finish();
                    }
                });

    }

}
